package com.nf.sms.comtroller;

import com.nf.sms.dao.ProductDAo;
import com.nf.sms.dao.impl.ProductDaoImpl;
import com.nf.sms.entity.Product;
import com.nf.sms.utils.PagerUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @AUTHOR CJ
 * <p>
 * *@DATE 2024 - 10 - 18
 */
@WebServlet("/pager_cond_more.do")
public class PagerBycondMoreServlet extends HttpServlet {
    private static final int PAGE_COUNT = 3;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        req.setCharacterEncoding("Utf-8");
        //获取页面传过来的当前页面
        String strCp = req.getParameter("cp");
        //必须保证当前页1-N的整数
        int cp = strCp !=null && strCp.matches("\\d{1,}" ) ? Integer.parseInt(strCp) : 1;

        //获取表单
        String productName = req.getParameter("productName");
        String productType = req.getParameter("productType");

        Product p = new Product();
        p.setProductType(productType);
        p.setProductName(productName);

        //第二：加工处理 - 实现具体的业务逻辑
        //1.实例化 Dao对象
        ProductDAo productDAo = new ProductDaoImpl();
        long totalRecordCount = productDAo.selectCount(p);

        //初步封装分页信息
        PagerUtil<Product> pagerUtil = new PagerUtil<>();
        pagerUtil.setPageCount(PAGE_COUNT);
        pagerUtil.setTotalRecordCount(totalRecordCount);
        pagerUtil.setCurrentPage(cp);

        List<Product> list = productDAo.selectByPage((int)pagerUtil.getCurrentPage(),PAGE_COUNT,p);
        pagerUtil.setData(list);

        req.setAttribute("pager",pagerUtil);
        req.getRequestDispatcher("pages_by_cond_more.jsp").forward(req,resp);
    }
}
